package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IActivityLike;
import com.happyclub.lbjdao.ActivityLike;
import com.happyclub.paul.dao.UserRepostitory;
import com.happyclub.pojo.TbActivityLike;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.List;

import static com.happyclub.errormsg.ErrorMsgProvider.SYSTEM_BUSY;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/8
 */
@Service
public class ActivityLikeImpl implements IActivityLike {
    @Autowired
    ActivityLike IactivityLike;
    
    @Autowired
    private UserRepostitory userRepostitory;

    @Override
    public JsonResult getAllActivityLike(String activityId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbActivityLike> activityLikes = IactivityLike.findTbActivityLikesByActivityId(activityId);
        if (activityLikes.size()!=0){
            for (TbActivityLike tc:activityLikes){
                TbUser tbUser = userRepostitory.queryUserById(tc.getUserId());
                tc.setHeadPortrait(tbUser.getHeadPortrait());
            }
            jsonResult.setResult(activityLikes);
            return jsonResult;
        }
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult saveActivityLike(TbActivityLike activityLike) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbActivityLike save = IactivityLike.save(activityLike);
        if (save==null){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg(SYSTEM_BUSY);
            return jsonResult;
        }
        //点赞成功后,实时展示出点赞人的头像...
        TbUser tbUser = userRepostitory.queryUserById(save.getUserId());
        save.setHeadPortrait(tbUser.getHeadPortrait());
        jsonResult.setResult(save);
        return jsonResult;
    }

    @Override
    public JsonResult checkUserIsLike(String activityId, String userId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbActivityLike tbActivityLike = IactivityLike.findByActivityIdAndUserId(activityId, userId);
        if (tbActivityLike==null){
            jsonResult.setMsg("未点赞");
            jsonResult.setResult(0);
            return jsonResult;
        }
        jsonResult.setMsg("已点赞");
        jsonResult.setResult(1);
        return jsonResult;
    }
}
